<template>
  <el-dialog title="商品查询" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <el-form inline :model="dataForm">
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.code"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="商品类别" prop="ctgId">
        <el-select v-model="dataForm.ctgId" placeholder="请选择">
          <el-option v-for="item in ctgOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓位名称">
        <el-select v-model="dataForm.stockId" disabled>
          <el-option v-for="item in stockOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="searchHandle">查询</el-button>
        <el-button type="info" plain @click="clearSearchForm">清空</el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="left">查询数据</el-divider>
    <el-row>
      <el-button type="primary" plain style="float: right;margin-bottom: 3px;" @click="checkAndClose">选择并关闭</el-button>
    </el-row>
    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" ref="goodsTable" border v-loading="dataListLoading"
        highlight-current-row @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="45"></el-table-column>
        <el-table-column key='1' prop="code" label="商品编码"></el-table-column>
        <el-table-column key='2' prop="name" label="商品名称"></el-table-column>
        <el-table-column key='3' prop="spec" label="型号" v-if="dataForm.ctgId ==1 ||dataForm.ctgId ==2 || dataForm.ctgId ==5 || dataForm.ctgId ==6 || dataForm.ctgId ==7 || dataForm.ctgId ==8 || dataForm.ctgId ==9"></el-table-column>
        <el-table-column key='4' prop="unitType" label="计量单位" :formatter="unitType"></el-table-column>
        <el-table-column key='5' prop="goodsPrice" label="销售价格"></el-table-column>
        <el-table-column key='6' prop="manufacturerColorNumber" label="厂家色号" v-if="dataForm.ctgId == 1 || dataForm.ctgId ==6 || dataForm.ctgId ==8"></el-table-column>
        <el-table-column key='7' prop="frameMaterial" label="镜架材质" v-if="dataForm.ctgId == 1 || dataForm.ctgId ==6"
          :formatter="formatter"></el-table-column>
        <el-table-column key='8' prop="goodsSize" label="商品尺寸" v-if="dataForm.ctgId == 1 || dataForm.ctgId ==6 || dataForm.ctgId ==8"></el-table-column>
        <el-table-column key='9' prop="accessoriesType" label="配件型" v-if="dataForm.ctgId == 2"
          :formatter="accessoriesType"></el-table-column>
        <el-table-column key='10' prop="mirrorBall" label="球镜" v-if="dataForm.ctgId == 3 || dataForm.ctgId == 4"></el-table-column>
        <el-table-column key='11' prop="columnMirror" label="柱镜" v-if="dataForm.ctgId == 3 || dataForm.ctgId == 4"></el-table-column>
        <el-table-column key='12' prop="toTheLight" label="下加光" v-if="dataForm.ctgId == 3"></el-table-column>
        <el-table-column key='13' prop="refractiveIndex" label="折射率" v-if="dataForm.ctgId == 3"></el-table-column>
        <el-table-column key='14' prop="lensMaterial" label="材料分类" v-if="dataForm.ctgId == 3"
          :formatter="lensMaterial"></el-table-column>
        <el-table-column key='15' prop="curvature" label="曲率" v-if="dataForm.ctgId == 4"></el-table-column>
        <el-table-column key='16' prop="diameter" label="直径" v-if="dataForm.ctgId == 4"></el-table-column>
        <el-table-column key='17' prop="useType" label="使用类型" v-if="dataForm.ctgId == 4"
          :formatter="useType"></el-table-column>
        <el-table-column key='18' prop="disusedClassification" label="抛弃分类" v-if="dataForm.ctgId == 4"
          :formatter="disusedClassification"></el-table-column>
        <el-table-column key='19' prop="primaryCapacity" label="主容量" v-if="dataForm.ctgId == 5"></el-table-column>
        <el-table-column key='20' prop="secondaryCapacity" label="次容量" v-if="dataForm.ctgId == 5"></el-table-column>
        <el-table-column key='21' prop="readingGlasses" label="老花镜度数" v-if="dataForm.ctgId == 8"></el-table-column>
        <el-table-column key='22' prop="stockName" label="仓位"></el-table-column>
        <el-table-column key='23' prop="storageQuantity" label="库存数量"></el-table-column>
        <el-table-column key='24' prop="batch" label="批次"></el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="avue-crud__pagination" style="margin-bottom: 10px;">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
  </el-dialog>
</template>

<script>
  import { getCheckGoodsPage } from '@/api/eims/check'
  import { getStorageCheckInfo } from '@/api/eims/storage'
  import { filterParams } from "@/util/tools"
  import { mapGetters } from "vuex"
  import { getCtgDataList } from "@/api/eims/check"
  import {
    getStockList,
    getStockInfo
  } from "@/api/eims/stock"

  export default {
    data() {
      return {
        visible: false,
        visible: false,
        dataForm: {},
        dataList: [],
        ctgOptions: [],
        stockOptions: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        checkType: '',
        selectList: []
      }
    },
    created() {
      this.stockList();
    },
    methods: {
      init(data) {
        this.visible = true
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
        this.dataList = []
        this.dataForm.stockId = data.paramsData.outStockId;
        this.tableData = data.tableData;
        this.getCtgList();
      },
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      //  清空数据表单
      clearSearchForm() {
        this.dataForm.code = '';
        this.dataForm.name = '';
      },
      getDataList() {
        if (!this.dataForm.ctgId) {
          this.$message.warning("请选择商品类别");
          return
        }
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        }
        let params2 = filterParams(this.dataForm)
        this.dataListLoading = true

        getStorageCheckInfo(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
            this.dataListLoading = false
            this.$nextTick(() => {
              if (this.tableData.length) {
                this.tableData.forEach(item => {
                  for (let i = 0; i < this.dataList.length; i++) {
                    if (item.goodsId == this.dataList[i].goodsId) {
                      this.$refs.goodsTable.toggleRowSelection(this.dataList[i], true);
                    }
                  }
                });
              }
            })
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      handleSelectionChange(val) {
        this.selectList = val
      },
      checkAndClose() {
        this.visible = false
        this.$emit('refreshForm', this.selectList)
        this.dataList = []
        this.totalPage = 0
      },
      getCtgList() {
        getCtgDataList().then(response => {
          if(response.data.code == '0'){
            let list = response.data.data.records
            let options = []
            list.forEach(item => {
              options.push({
                value: item.id,
                name: item.name
              })
            })
            this.ctgOptions = options
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  获取仓位列表
      stockList() {
        getStockList().then(response => {
          if(response.data.code == '0'){
            this.stockOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      formatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '合金'
            break
          case 2:
            return '钛合金'
            break
          case 3:
            return 'K金'
            break
          case 4:
            return '板材'
            break
          case 5:
            return 'TR-9'
            break
          case 6:
            return '钨碳'
            break
          case 7:
            return 'β钛'
            break
          case 8:
            return '纯钛'
            break
          case 9:
            return '混搭'
            break
          case 10:
            return '太阳钴'
            break
          case 11:
            return '聚酰胺'
            break
          case 12:
            return '包金'
            break
        }
      },
      accessoriesType(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '框镜'
            break
          case 2:
            return '隐形'
            break
        }
      },
      lensMaterial(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '树脂'
            break
          case 2:
            return '玻璃'
            break
          case 3:
            return 'PC'
            break
        }
      },
      usrType(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '常带型'
            break
          case 2:
            return '抛弃型'
            break
          case 3:
            return '塑形镜'
            break
        }
      },
      disusedClassification(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '日抛'
            break
          case 2:
            return '周抛'
            break
          case 3:
            return '月抛'
            break
          case 4:
            return '季抛'
            break
          case 5:
            return '半年抛'
            break
          case 6:
            return '年抛'
            break
          case 7:
            return 'RGP'
            break
          case 8:
            return '塑形镜'
            break
          case 9:
            return '双周抛'
            break
        }
      },
      unitType(row, column, cellValue, index) {
        switch (cellValue) {
          case '01':
            return '片';
            break;
          case '02':
            return '盒';
            break;
          case '03':
            return '副';
            break;
          case '04':
            return '瓶';
            break;
          case '05':
            return '张';
            break;
          case '06':
            return '把';
            break;
          case '07':
            return '个';
            break;
          case '08':
            return '只';
            break;
          case '09':
            return '套';
            break;
          case '10':
            return '天';
            break;
          case '11':
            return '包';
            break;
        }
      },
    }
  };
</script>
